package com.example.httpdemo;

import android.Manifest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.httpdemo.R;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends AppCompatActivity {

    private TextView tvData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String[]perms={Manifest.permission.INTERNET};
        if(!EasyPermissions.hasPermissions(this,perms)){
            EasyPermissions.requestPermissions(this,"internet",1,perms);
        }


        //找控件
        tvData = (TextView) findViewById(R.id.tv_data);
        Button btnPost = (Button) findViewById(R.id.btn_post);
        btnPost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //使用post方式请求服务器
                        URL url = null;
                        try {
                            url = new URL("http://10.0.2.2:8080/MyServer/Login");
                            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                            conn.setConnectTimeout(5000);
                            conn.setRequestMethod("POST");
                            String data = "username="+ URLEncoder.encode("admin")
                                    +"&password="+URLEncoder.encode("123");
                            conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
                            conn.setRequestProperty("Content-Length",data.length()+"");
                            conn.setDoOutput(true);
                            //Thread.sleep(3000);
                            OutputStream os = conn.getOutputStream();
                            os.write(data.getBytes());
                            int code = conn.getResponseCode();
                            //Thread.sleep(3000);
                            if(code == 200){
                                //获取服务器的验证结果
                                //Thread.sleep(3000);
                                InputStream is = conn.getInputStream();
                                byte[]buf = new byte[1024];
                                is.read(buf);
                                final String recdata =new String(buf);
                                Log.i("MainActivity","rec="+recdata);
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        tvData.setText(recdata);
                                    }
                                });
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

            }
        });
    }
}
